package gofish.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import gofish.dao.DAOException;
import gofish.dao.GoFishDAOHandler;
import gofish.json.JsonObject;

/**
 * StartGame will be a Servlet that Adam's client
 * code can contact. I believe this will be used
 * to get a game started once enough people have
 * joined the group.
 *
 * @author Matt Lauber
 */
public class StartGame extends HttpServlet {

	/**
	 * IMPLEMENT AND FINISH DOCUMENTATION!
	 */
	@Override
	//public int doPost() {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		response.setContentType("application/json");

		PrintWriter out = response.getWriter();
		try {
		JsonObject jsonResponse = new JsonObject();
		int gid = Integer.parseInt(request.getParameter("gid"));
		GoFishDAOHandler handler = new GoFishDAOHandler(getServletContext());
		
		handler.startGame(gid);
		
		/* FOR NOW JUST RETURN TO HIM THESE HARD-CODED VALUES */
		jsonResponse.add("success", true).add("gid", 1);
		//jsonResponse.add("success", true);

		out.print(jsonResponse.toString());
		} catch (Exception e) {
			JsonObject json = new JsonObject();
			StringBuilder builder = new StringBuilder();
			for (StackTraceElement elem : e.getStackTrace()) {
				builder.append(elem.toString() + " ");
			}
			json.add("error", builder.toString());
			out.print(json.toString());
		}
		out.flush();
	}
}
